/**
 * Created with IntelliJ IDEA.
 * Description:给定一个二叉树 root ，返回其最大深度。
 *
 * 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
 * User: wangxin
 * Date: 2025-04-07
 * Time: 21:05
 */
 class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class Test {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int res = func(root,0);
        return res;
    }
    public int func(TreeNode root,int max){
        if(root == null){
            return 0;
        }
        int left = func(root.left,max);
        int right = func(root.right,max);
        max = max + Math.max(left,right)+1;
        return max;
    }
}
